package com.yunxin.core.infra.persistence.repository.product;

import com.yunxin.core.domain.model.product.ProductThrowInConfig;
import com.yunxin.core.domain.model.product.ProductThrowInConfigRepository;
import com.yunxin.core.infra.persistence.converter.ProductConverter;
import com.yunxin.core.infra.persistence.entity.product.ProductThrowInConfigDOWithBLOBs;
import com.yunxin.core.infra.persistence.mapper.product.ProductThrowInConfigExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zero°
 * @since 2023-07-13
 */
@Repository
@RequiredArgsConstructor
public class ProductThrowInConfigRepositoryImpl implements ProductThrowInConfigRepository {

    private final ProductThrowInConfigExtendMapper productThrowInConfigExtendMapper;

    private final ProductConverter productConverter;

    @Override
    public boolean saveProductThrowInConfig(ProductThrowInConfig productThrowInConfig) {
        ProductThrowInConfigDOWithBLOBs productThrowInConfigDO = productConverter.formProductThrowInConfig(productThrowInConfig);
        productThrowInConfigDO.setUserProtocol(productThrowInConfig.getUserProtocol());
        int count = productThrowInConfigExtendMapper.insertSelective(productThrowInConfigDO);
        return count > 0;
    }

    @Override
    public boolean updateProductThrowInConfig(ProductThrowInConfig productThrowInConfig) {
        ProductThrowInConfigDOWithBLOBs productThrowInConfigDO = productConverter.formProductThrowInConfig(productThrowInConfig);
        productThrowInConfigDO.setUserProtocol(productThrowInConfig.getUserProtocol());
        int count = productThrowInConfigExtendMapper.updateByPrimaryKeySelective(productThrowInConfigDO);
        return count > 0;
    }

    @Override
    public ProductThrowInConfig getProductThrowInConfigByProductId(Long productId) {
        ProductThrowInConfigDOWithBLOBs productThrowInConfigDO = productThrowInConfigExtendMapper.selectByProductId(productId);
        return productConverter.toProductThrowInConfig(productThrowInConfigDO);
    }

    @Override
    public List<ProductThrowInConfig> getProductThrowInConfigByBeProductId(Long productId) {
        List<ProductThrowInConfigDOWithBLOBs> productThrowInConfigDO = productThrowInConfigExtendMapper.selectByBeProductId(productId);

        ArrayList<ProductThrowInConfig> configLIst = new ArrayList<>();
        productThrowInConfigDO.forEach(dto->{
            configLIst.add(productConverter.toProductThrowInConfig(dto));
        });

        return configLIst;
    }
}
